package com.supostacks.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supostacks.domain.repository.IResourceInfoRepository;
import com.supostacks.domain.vo.ResourceInfoVo;
import com.supostacks.infrastructure.context.DefaultContext;
import com.supostacks.infrastructure.context.EntityUtil;
import com.supostacks.infrastructure.dao.CategoryDao;
import com.supostacks.infrastructure.dao.ResourceInfoDao;
import com.supostacks.infrastructure.po.Category;
import com.supostacks.infrastructure.po.ResourceInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class ResourceInfoRepository extends DefaultContext implements IResourceInfoRepository {

    @Resource
    private ResourceInfoDao resourceInfoDao;
    @Resource
    private CategoryDao categoryDao;

    @Override
    public String insert(ResourceInfoVo resourceInfoVo) {
        try {
            ResourceInfo resourceInfo = new ResourceInfo();
            EntityUtil.copyFields(resourceInfoVo, resourceInfo);

            int insert = resourceInfoDao.insert(resourceInfo);
            if (insert > 0) {
                return resourceInfo.getId();
            }
            return null;

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        resourceInfoDao.deleteById(id);
    }

    @Override
    public String updateById(ResourceInfoVo resourceInfoVo) {
        try {
            ResourceInfo resourceInfo = new ResourceInfo();
            EntityUtil.copyFields(resourceInfoVo, resourceInfo);
            int i = resourceInfoDao.updateById(resourceInfo);
            if (i > 0) {
                return resourceInfo.getId();
            }
            return "";
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResourceInfoVo selectById(String id) {
        ResourceInfo resourceInfo = resourceInfoDao.selectById(id);
        if (resourceInfo != null) {
            return poToVo(resourceInfo);
        }
        return null;
    }

    @Override
    public JSONObject selectByPage(JSONObject options, int pageNo, int pageSize) {
        QueryWrapper<ResourceInfo> queryWrapper = where(options);

        Page<ResourceInfo> infos = new Page<>(pageNo, pageSize);
        IPage<ResourceInfo> iPage = resourceInfoDao.selectPage(infos, queryWrapper);

        List<ResourceInfo> records = iPage.getRecords();
        List<ResourceInfoVo> records1 = buildList(records);

        JSONObject result = new JSONObject();
        result.put("total", iPage.getTotal());
        result.put("list", records1);

        return result;
    }

    @Override
    public List<ResourceInfoVo> selectList(JSONObject param) {
        QueryWrapper<ResourceInfo> queryWrapper = where(param);
        List<ResourceInfo> resourceInfos = resourceInfoDao.selectList(queryWrapper);
        return buildList(resourceInfos);
    }

    public List<ResourceInfoVo> buildList(List<ResourceInfo> resourceInfos) {
        return resourceInfos.stream().map(this::poToVo).collect(Collectors.toList());
    }

    public ResourceInfoVo poToVo(ResourceInfo resourceInfo) {
        try {
            ResourceInfoVo resourceInfoVo = new ResourceInfoVo();
            EntityUtil.copyFields(resourceInfo, resourceInfoVo);
            fillData(resourceInfoVo);
            return resourceInfoVo;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public Category takeCategory(String categoryId) {
        return categoryDao.selectById(categoryId);
    }

    public void fillData(ResourceInfoVo resourceInfoVo) {

        Category category = takeCategory(resourceInfoVo.getCategoryId());
        if (category != null) {
            resourceInfoVo.setCategoryName(category.getCategoryName());
        }

    }
}
